﻿using Model;
using System.Collections.Generic;

namespace BLL
{
    public class Yx_CustomerBankBLL : EntityBLL<Yx_CustomerBank>
    {
        public override bool Insert(Yx_CustomerBank entity)
        {
            entity.N_ID = GetSequence("SEQ_N_CUSTOMER_BANK_ID");
            var sql = @"INSERT INTO YX_CUSTOMER_BANK(
	N_ID,
	N_CUSTOMER_ID,
	C_REALNAME,
	C_NUMBER,
	N_BANK_ID,
	D_OPERATOR_DATE,
	N_OPERATOR_ID
)
VALUES(
	{0},
	{1},
	'{2}',
	'{3}',
	{4},
	TO_DATE('{5}','YYYY-MM-DD HH24:MI:SS'),
	{6}
)";
            sql = string.Format(sql, entity.N_ID, entity.N_CUSTOMER_ID, entity.C_REALNAME, entity.C_NUMBER, entity.N_BANK_ID, entity.D_OPERATOR_DATE.ToString("yyyy-MM-dd HH:mm:ss"), entity.N_OPERATOR_ID);
            return NoQuery(sql) > 0 ? true : false;
        }

        public override bool Update(Yx_CustomerBank entity)
        {
            var sql = @"UPDATE YX_CUSTOMER_BANK
SET
	N_CUSTOMER_ID = {1},
	C_REALNAME = '{2}',
	C_NUMBER = '{3}',
	N_BANK_ID = {4},
	D_OPERATOR_DATE = TO_DATE('{5}','YYYY-MM-DD HH24:MI:SS'),
	N_OPERATOR_ID = {6}
WHERE
	N_ID = {0}";
            sql = string.Format(sql, entity.N_ID, entity.N_CUSTOMER_ID, entity.C_REALNAME, entity.C_NUMBER, entity.N_BANK_ID, entity.D_OPERATOR_DATE.ToString("yyyy-MM-dd HH:mm:ss"), entity.N_OPERATOR_ID);
            return NoQuery(sql) > 0 ? true : false;
        }

        public override List<Yx_CustomerBank> Where(string whereStr = null, string orderbyStr = null, int? skip = null, int? take = null)
        {
            var sql = "SELECT * FROM YX_CUSTOMER_BANK";
            if (whereStr != null)
            {
                sql += " WHERE " + whereStr;
            }
            if (orderbyStr != null)
            {
                sql += " ORDER BY " + orderbyStr;
            }
            var dt = PageQuery(sql, skip, take);
            return ConvertEntity(dt);
        }

        public override Yx_CustomerBank Find(object key)
        {
            var data = Where("N_ID = " + key.ToString());
            if (data.Count == 1)
            {
                return data[0];
            }
            return null;
        }
    }
}